/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.utils;

import com.gargoylesoftware.htmlunit.StringWebResponse;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebConnection;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.util.FalsifyingWebConnection;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Rainbow
 */
public class WebClientModifier extends FalsifyingWebConnection
{
    Logger myLogger = LoggerFactory.getLogger(WebClientModifier.class);
    public WebClientModifier(WebClient webClient)
    {
        super(webClient);
    }
    
        @Override
    public WebResponse getResponse(WebRequest settings) throws IOException
    {
        return processResponse(settings);
    }
    
        private WebResponse processResponse(WebRequest settings) throws IOException
    {
        //replace response 
        if (settings.getUrl().toExternalForm().startsWith("http://pos.baidu.com/ecom?"))
        {
            myLogger.debug("Banned url in flib: http://pos.baidu.com/ecom?");
            return createWebResponse(settings, "", "text/javascript");
        }
        if (settings.getUrl().toExternalForm().startsWith("http://d1.sina.com.cn/litong/zhitou/sspnew.js"))
        {
            myLogger.debug("Banned url in flib: http://d1.sina.com.cn/litong/zhitou/sspnew.js");
            return createWebResponse(settings, "", "text/javascript");
        }
        if (settings.getUrl().toExternalForm().startsWith("http://static.bshare.cn/b/buttonLite.js"))
        {
            myLogger.debug("Banned url in flib: http://static.bshare.cn/b/buttonLite.js");
            return createWebResponse(settings, "", "text/javascript");
        }
        if (settings.getUrl().toExternalForm().startsWith("http://static.bshare.cn/b/bshareC0.js"))
        {
            myLogger.debug("Banned url in flib: http://static.bshare.cn/b/bshareC0.js");
            return createWebResponse(settings, "", "text/javascript");
        }
        if (settings.getUrl().toExternalForm().contains(".fbcdn.net"))
        {
            myLogger.debug("Banned url in flib: " + settings.getUrl());
            return createWebResponse(settings, "", "text/javascript");
        }
        else if (settings.getUrl().toExternalForm().startsWith("http://static.baifendian.com/api/2.0/bcore.min.js"))
        {
            myLogger.debug("Banned url in flib: http://static.baifendian.com/api/2.0/bcore.min.js");
            return createWebResponse(settings, "", "text/javascript");
        }
        else if (settings.getUrl().toExternalForm().startsWith("http://adi2007.cnool.net/AdShowCache?bbs-qzyctg|1|2328"))
        {
            myLogger.debug("Banned url in flib: http://adi2007.cnool.net/AdShowCache?bbs-qzyctg|1|2328");
            return createWebResponse(settings, "", "text/javascript");
        }
        else if (settings.getUrl().toExternalForm().equals("http://nb8185.cnnb.com.cn/tiezi/js/jquery_con.js"))
        {
            myLogger.debug("Banned url in flib: http://nb8185.cnnb.com.cn/tiezi/js/jquery_con.js");
            return createWebResponse(settings, "", "text/javascript");
        }
        else if (settings.getUrl().toExternalForm().equals("http://nb8185.cnnb.com.cn/tiezi/js/totop_ms.js"))
        {
            myLogger.debug("Banned url in flib: http://nb8185.cnnb.com.cn/tiezi/js/totop_ms.js");
            return createWebResponse(settings, "", "text/javascript");
        }
        else if (settings.getUrl().toExternalForm().startsWith("http://cpro.baidu.com/cpro/"))
        {
            myLogger.debug("Banned url in flib: http://cpro.baidu.com/cpro/");
            return createWebResponse(settings, "", "text/javascript");
        }
        else if (settings.getUrl().toExternalForm().equals("http://www.newsmth.net/nForum/baiduunion/article"))
        {
            myLogger.debug("Banned url in flib: http://www.newsmth.net/nForum/baiduunion/article");
            return createWebResponse(settings, "", "text/javascript");
        }
        else if (settings.getUrl().toExternalForm().startsWith("http://m.weather.com.cn"))
        {
            myLogger.debug("Banned url in flib: http://m.weather.com.cn");
            return createWebResponse(settings, "", "text/javascript");
        }
        else if (settings.getUrl().toExternalForm().startsWith("http://rs1.bbs.itc.cn/v201401101800/?v=2.13130&q=/jquery1.6.js"))
        {
            myLogger.debug("Banned url in flib: http://rs1.bbs.itc.cn/v201401101800/?v=2.13130&q=/jquery1.6.js");
            return createWebResponse(settings, "", "text/javascript");
        }
        else if (settings.getUrl().toExternalForm().startsWith("http://i.tianqi.com"))
        {
            myLogger.debug("Banned url in flib: http://i.tianqi.com");
            return createWebResponse(settings, "", "text/javascript");
        }
        else if (settings.getUrl().toExternalForm().startsWith("http://www.google-analytics.com/ga.js"))
        {
            myLogger.debug("Banned url in flib: http://www.google-analytics.com/ga.js");
            return createWebResponse(settings, "", "text/javascript");
        }
        else if (settings.getUrl().toExternalForm().startsWith("http://web8.eyuyao.com/yyad/"))
        {
            myLogger.debug("Banned url in flib: " + settings.getUrl().toExternalForm());
            return createWebResponse(settings, "", "text/javascript");
        }
        else if (settings.getUrl().toExternalForm().startsWith("http://hm.baidu.com/"))
        {
            myLogger.debug("Banned url in flib: " + settings.getUrl().toExternalForm());
            return createWebResponse(settings, "", "text/javascript");
        }
        else if (settings.getUrl().toExternalForm().startsWith("http://nsclick.baidu.com/"))
        {
            myLogger.debug("Banned url in flib: " + settings.getUrl().toExternalForm());
            return createWebResponse(settings, "", "text/javascript");
        }
        else if (settings.getUrl().toExternalForm().startsWith("http://googleads.g.doubleclick.net/"))
        {
            myLogger.debug("Banned url in flib: " + settings.getUrl().toExternalForm());
            return createWebResponse(settings, "", "text/javascript");
        }
        else if (settings.getUrl().getHost().endsWith(".cnzz.com"))
        {
            myLogger.debug("Banned url in flib: " + settings.getUrl().toExternalForm());
            return createWebResponse(settings, "", "text/javascript");
        }
        else if (settings.getUrl().getHost().endsWith(".tianqi.com"))
        {
            myLogger.debug("Banned url in flib: " + settings.getUrl().toExternalForm());
            return createWebResponse(settings, "", "text/javascript");
        }
        
        //modify response
        WebResponse wr = super.getResponse(settings);
        String url = settings.getUrl().toExternalForm();
        if (url.startsWith("http://www.xm168shop.com/thread"))
        {
            String s = wr.getContentAsString("GBK");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://www.chinanews.com"))
        {
            String s = wr.getContentAsString("GBK");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://paper.cnstock.com"))
        {
            String s = wr.getContentAsString("UTF-8");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://www.eclianmeng.com/s.php"))
        {
            String s = wr.getContentAsString("GBK");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://bbs.yygr.cn/viewthread.php"))
        {
            String s = wr.getContentAsString("UTF-8");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://www.he168.cn/i.php"))
        {
            String s = wr.getContentAsString("GBK");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://b.fzengine.com/view"))
        {
            String s = wr.getContentAsString("GBK");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://www.xsnet.cn/"))
        {
            String s = wr.getContentAsString("GBK");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://www.zjbts.gov.cn/HTML"))
        {
            String s = wr.getContentAsString("GBK");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://www.bjtsb.gov.cn"))
        {
            String s = wr.getContentAsString("GBK");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://wzrb.66wz.com/"))
        {
            String s = wr.getContentAsString("UTF-8");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://ad.qianlong.com")
                || url.startsWith("http://wap.cmread.com/"))
            wr = createWebResponse(settings, "", "text/html");
        else if (url.startsWith("http://www.zjnongbao.com"))
        {
            String s = wr.getContentAsString("GBK");
            wr = HtmlTools.createWebResponse(settings, s, "text/html", wr.getResponseHeaders());
        }
        else if (url.startsWith("http://yz.cnnb.com.cn"))
        {
            String s = wr.getContentAsString("gbk");
            wr = HtmlTools.createWebResponse(settings, s, "text/html", wr.getResponseHeaders());
        }
        else if (url.startsWith("http://qzpd.zjol.com.cn"))
        {
            String s = wr.getContentAsString("gb2312");
            wr = HtmlTools.createWebResponse(settings, s, "text/html", wr.getResponseHeaders());
        }
        else if (url.startsWith("http://www.xmnn.cn/sjmpl/"))
        {
            String s = wr.getContentAsString("GB2312");
            wr = HtmlTools.createWebResponse(settings, s, "text/html", wr.getResponseHeaders());
        }
        else if (url.startsWith("http://www.hzgcc.org"))
        {
            String s = wr.getContentAsString("gbk");
            wr = HtmlTools.createWebResponse(settings, s, "text/html", wr.getResponseHeaders());
        }
        else if (url.startsWith("http://www.yqnews.com.cn/"))
        {
            String s = wr.getContentAsString("GB2312");
            wr = HtmlTools.createWebResponse(settings, s, "text/html", wr.getResponseHeaders());
        }
        else if (url.startsWith("http://www.jishigang.nbyz.gov.cn/"))
        {
            String s = wr.getContentAsString("GB2312");
            wr = HtmlTools.createWebResponse(settings, s, "text/html", wr.getResponseHeaders());
        }
        else if (url.startsWith("http://dsb.66wz.com/") || url.startsWith("http://wzed.66wz.com/") || url.startsWith("http://wzwb.66wz.com"))
        {
            String s = wr.getContentAsString("UTF-8");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://news.cnnb.com.cn"))
        {
            String s = wr.getContentAsString("GBK");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://www.dahuawang.com/"))
        {
            String s = wr.getContentAsString("UTF-8");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://www.lcxwfc.com/"))
        {
            String s = wr.getContentAsString("GB2312");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://www.safete.org.cn"))
        {
            String s = wr.getContentAsString("GB2312");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://www.66lc.com"))
        {
            String s = wr.getContentAsString("GBK");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://tjj.ningbo.gov.cn"))
        {
            //宁波市统计局
            String s = wr.getContentAsString("GBK");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://www.xnnews.com.cn"))
        {
            //咸宁新闻网
            String s = wr.getContentAsString("GBK");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://www.jxga.gov.cn"))
        {
            //江西省公安厅
            String s = wr.getContentAsString("GBK");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://www.peacehall.com"))
        {
            //博讯新闻网
            String s = wr.getContentAsString("GBK");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://house.anhuinews.com"))
        {
            //中安在线
            String s = wr.getContentAsString("GBK");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://weixin.sogou.com"))
        {
            String s = wr.getContentAsString("UTF-8");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://www.zgshengsi.com"))
        {
            //嵊泗网
            String s = wr.getContentAsString("GBK");
            wr = createWebResponse(settings, s, "text/html");
        }
        else if (url.startsWith("http://www.nbrb.com.cn"))
        {
            //宁波日报网
            String s = wr.getContentAsString("GBK");
            wr = createWebResponse(settings, s, "text/html");
        }
        if (url.startsWith("http://img.") || url.startsWith("http://img1.") || url.startsWith("http://img2.") || url.startsWith("http://img3.") || settings.getUrl().getFile().endsWith(".css") || settings.getUrl().getFile().endsWith(".jpg")
                || settings.getUrl().getFile().endsWith(".gif")
                || settings.getUrl().getFile().endsWith(".png"))
        {
            return new StringWebResponse("", settings.getUrl());
        }
        return wr;
    }
    
}
